package com.mmsj;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


import androidx.appcompat.app.AppCompatActivity;

import com.mmsj.pojo.MyUser;
import com.mmsj.service.ApiService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_retrofit);
        Button btn1 = findViewById(R.id.btn_1);
        Button btn2 = findViewById(R.id.btn_2);
        Button btn3 = findViewById(R.id.btn_3);
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl("http://192.168.124.2:9292/")  // 基础URL
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
                // 2. 创建接口实例
                ApiService api = retrofit.create(ApiService.class);
                MyUser user = new MyUser();
                user.setId(300);
                user.setName("aaa");
                Call<MyUser> call = api.addUser(user);
                call.enqueue(new Callback<MyUser>() {
                    @Override
                    public void onResponse(Call<MyUser> call, Response<MyUser> response) {
                        Log.d("myLog", "请求成功");
                        if (response.isSuccessful()) {
                            MyUser myUser = response.body();

                            Toast.makeText(RetrofitActivity.this, "新增成功", Toast.LENGTH_SHORT).show();

                        }
                    }

                    @Override
                    public void onFailure(Call<MyUser> call, Throwable t) {
                        Log.d("myLog", "请求失败：" + t.toString());
                    }
                });
            }
        });
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl("http://192.168.124.2:9292/")  // 基础URL
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
                // 2. 创建接口实例
                ApiService api = retrofit.create(ApiService.class);
                Call<MyUser> call = api.getUserById(100);
                call.enqueue(new Callback<MyUser>() {
                    @Override
                    public void onResponse(Call<MyUser> call, Response<MyUser> response) {
                        if (response.isSuccessful()) {
                            MyUser myUser = response.body();
                            Log.d("myLog", "用户ID=" + myUser.getId() + ",用户姓名=" + myUser.getName());
                            Toast.makeText(RetrofitActivity.this, "查询数据成功", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<MyUser> call, Throwable t) {

                    }
                });
            }
        });
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 发送http请求到后台获取所有用户信息：http://127.0.0.1:8080/user/getAllUser
                // 1.创建retrofit的构建器，通过Builder配置基础的URL和解析器
                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl("http://192.168.124.2:9292/")  // 基础URL
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
                // 2. 创建接口实例
                ApiService api = retrofit.create(ApiService.class);
                // 3.调用接口
                Call<List<MyUser>> call = api.getAllUser();
                // 4.在回调函数中解析后端返回的数据
                call.enqueue(new Callback<List<MyUser>>() {
                    @Override
                    public void onResponse(Call<List<MyUser>> call, Response<List<MyUser>> response) {
                        if (response.isSuccessful()) {
                            // 从response解析数据
                            List<MyUser> myUsers = response.body();
                            for (MyUser user : myUsers) {
                                Log.d("myLog", "用户ID=" + user.getId() + ",用户姓名=" + user.getName());
                            }
                            Toast.makeText(RetrofitActivity.this, "查询数据成功", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<List<MyUser>> call, Throwable t) {
                        Log.e("myLog", "查询数据失败:" + t.getMessage());
                        Toast.makeText(RetrofitActivity.this, "查询数据失败", Toast.LENGTH_SHORT).show();
                    }
                });

            }
        });


    }
}